<template>
	<view class="p-20">

		<view class="bg-black-1 mb-20 p-15 round-10">
			<view class="flex text-white">
				<text class="flex-1">已连续签到{{sign.continuous}}天</text>
				<view>
					<view class=" round-30  py-4 px-12 fs-12 " style="background-color: #fb9009;" @click="userSign">签到
					</view>
				</view>
			</view>
			<view class="grid-columns-7 mt-10">
				<view class=" w-40 h-60" v-for="(item,index) in 7" :key='index'>
					<view v-if='item <= sign.continuous' class="w-40 h-60 round-6 box flex flex-column flex-center">
						<image src="@/static/icon/jb.png" class="w-25 h-25"></image>
						<text class="text-white fs-12"
							v-if='item % WEBCONFIG?.sign_continuous ==0'>+{{WEBCONFIG?.sign_continuous_points}}</text>
						<text class="text-white fs-12" v-else>+{{WEBCONFIG?.sign_points}}</text>
					</view>
					<view v-else class=" w-40 h-60 flex box-1 flex-column round-6 flex-center">
						<image src="@/static/icon/bx.png" class="w-25 h-25"></image><text class="text-white fs-12"
							v-if='item % WEBCONFIG?.sign_continuous ==0'>+{{WEBCONFIG?.sign_continuous_points}}</text>
						<text class="text-white fs-12" v-else>+{{WEBCONFIG?.sign_points}}</text>
					</view>
				</view>

			</view>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view v-if="WEBCONFIG?.weixin_video_status && WEBCONFIG?.weixin_video_status == 1">
			<ad :unit-id="WEBCONFIG?.weixin_video_ad_id" ad-theme="white"></ad>
		</view>
		<!-- #endif -->
		<view class="text-white parent bg-black-1 round-6 ">
			<view class="flex flex-y-center p-10">
				<view class="round-circle flex flex-center w-40 h-40 bg-blue flex-shrink">
					<text class=" iconfont icon-paperPlane-fill fs-25"></text>
				</view>
				<view class="flex flex-column grid-gap-2 flex-1 ml-8">
					<text>邀请好友</text>
					<text class="fs-12 text-666">邀请一位新用户即可获得{{WEBCONFIG?.invite_points}}点数</text>
				</view>
				<view>
					<button class="btn-normal" open-type="share">
						<view class="bg-blue text-white round-30 py-4 px-8 fs-13">分享</view>
					</button>
				</view>
			</view>
			<!-- #ifdef MP-WEIXIN -->
			<view class="flex flex-y-center p-10">
				<view class="round-circle flex flex-center w-40 h-40 bg-blue flex-shrink">
					<text class=" iconfont icon-guankanshipin1 fs-30"></text>
				</view>
				<view class="flex flex-column grid-gap-2 flex-1 ml-8">
					<text>观看视频 {{taskInfo.video_num}}/{{WEBCONFIG?.watch_number}}</text>
					<text class="fs-12 text-666">每观看一次获得{{WEBCONFIG?.watch_points}}点数</text>
				</view>
				<view>
					<view class="bg-blue text-white round-30 py-4 px-8 fs-13" @click="play()">观看</view>
				</view>
			</view>
			<!-- #endif -->

			<!-- #ifdef MP-WEIXIN -->
			<view class="flex flex-y-center p-10" v-for="(item,index) in navList" :key='index'>
				<image class="round-circle   w-40 h-40  flex-shrink" :src='item.image'>
				</image>
				<view class="flex flex-column grid-gap-2 flex-1 ml-8">
					<text>{{item.name}}</text>
					<text class="fs-12 text-666 text-ellipsis-1">试玩{{item.task_time}}秒即可获得{{item.points}}点数</text>
				</view>
				<view>
					<view class="bg-blue text-white round-30 py-4 px-8 fs-13" @click="toApplet(item)">试玩</view>
				</view>
			</view>
			<!-- #endif -->

			<!-- #ifdef H5 -->
			<view class="flex flex-y-center p-10" v-for="(item,index) in navList" :key='index'>

				<image class="round-circle   w-40 h-40  flex-shrink" :src='item.image'>
				</image>
				<view class="flex flex-column grid-gap-2 flex-1 ml-8">
					<text>{{item.name}}</text>
					<text class="fs-12 text-666 text-ellipsis-1">试玩{{item.task_time}}秒即可获得{{item.points}}点数</text>
				</view>
				<view class="position-relative">
					<view class="bg-blue text-white round-30 py-4 px-8 fs-13" @click="toApplet(item)">试玩</view>
					<wx-open-launch-weapp :appid="item.appid" :path="item.path" id="launch-btn"
						style="position: absolute;inset: 0;width: 100%;height: 100%;" @click="toApplet(item)">
						<component :is="'script'" type="text/wxtag-template">
							<view style="position: absolute;inset: 0;width: 100%;height: 100%;">

							</view>
						</component>
					</wx-open-launch-weapp>
				</view>
			</view>
			<!-- #endif -->

		</view>


		<!-- #ifdef MP-WEIXIN -->
		<view v-if="WEBCONFIG?.weixin_banner_status && WEBCONFIG?.weixin_banner_status == 1">
			<ad :unit-id="WEBCONFIG?.weixin_banner_ad_id" ad-theme="white"></ad>
		</view>
		<!-- #endif -->
	</view>
</template>

<script setup>
	import { onShow, onLoad, onShareAppMessage } from '@dcloudio/uni-app'
	import { reactive, ref } from 'vue';
	import { $http, $tool, useStorage } from '@/utils';
	import { useUserStore, useRefs, useWebConfigStore } from '@/stores'
	import { $interstitialAd, $videoAd } from '@/utils/ad'
	import system from "@/utils/system.js";
	const { USERINFO } = useRefs(useUserStore())
	const { WEBCONFIG } = useRefs(useWebConfigStore())
	const storage = useStorage();

	const time = ref(0)
	const status = ref(false)
	const navList = ref([])
	const key = reactive('YCQRCODE_TASK')
	const sign = ref({ continuous: 0 })
	const navDetail = ref()
	const taskInfo = ref({
		video_num: 0,
		nav_ids: []
	})
	onLoad(() => {
		// #ifdef MP-WEIXIN
		if (WEBCONFIG.value?.weixin_incentive_status == '1') {
			$videoAd.init(WEBCONFIG.value?.weixin_incentive_ad_id, (e) => {
				if (e && e.isEnded || e === undefined) {
					completeTask()
				} else {
					$tool.toast('未观看完视频,无法获得奖励');
				}
			})
		}
		// #endif
		getNav()
		getSignDetail()
	})
	onShow(() => {
		validateTask()
		initTask()

		// #ifdef H5
		$tool.replaceUrlParam('appid', system.appid());
		let user_id = USERINFO.value?.id
		if (user_id) {
			$tool.replaceUrlParam('user_id', user_id)
		}
		// #endif
	})

	const validateTask = () => {
		// #ifdef H5
		if (status.value == true) {
			if (time.value < navDetail.value.task_time) {
				uni.showModal({
					title: '温馨提示',
					content: '时长未达到'
				})
			} else {
				completeTask('2', navDetail.value.id)
			}

			time.value = 0
		}
		// #endif
		// #ifdef MP-WEIXIN
		if (uni.getEnterOptionsSync().scene == 1038 && status.value == true) {
			if (time.value < navDetail.value.task_time) {
				uni.showModal({
					title: '温馨提示',
					content: '时长未达到'
				})
			} else {
				completeTask('2', navDetail.value.id)
			}
			time.value = 0
		}
		// #endif

		status.value = false
	}
	const initTask = () => {
		let res = storage.get(key);
		const time = getExpireTime();
		const now = Date.now() / 1000;
		if (!res || now > res.expire_time) {
			res = {
				expire_time: time,
				data: taskInfo.value,
			};
		} else {
			taskInfo.value = res.data;
		}

		storage.set(key, res);
	};



	const getExpireTime = () => {
		const now = new Date();
		now.setHours(23, 59, 59, 999);
		return Math.floor(now.getTime() / 1000);
	}


	const completeTask = (type = '1', nav_id = 0) => {
		$http.post('Task/complete', { type: type, nav_id: nav_id }).then(res => {
			let task = storage.get(key)
			$tool.toast('任务完成');
			if (type == '1') {
				taskInfo.value.video_num += 1
			}
			if (type == '2') {
				taskInfo.value.nav_ids.push(nav_id)
			}
			task.data = taskInfo.value
			storage.set(key, task)
		})

	}


	const getSignDetail = () => {
		$http.get('Sign/getDetail').then(res => {
			if (res.data) {
				sign.value = res.data
			}
		})
	}

	const userSign = () => {
		$http.get('Sign/sign').then(res => {
			$tool.toast('签到成功')
			getSignDetail()
		})
	}

	const getNav = () => {
		$http.get('Nav/getList').then(res => {
			navList.value = res.data
		})
	}


	const toApplet = (e) => {
		navDetail.value = e
		time.value = 0
		// #ifdef H5
		if (status.value == false && $tool.isWeChatEnv()) {
			status.value = true
			timeAdd()
		}
		// #endif
		// #ifdef MP-WEIXIN
		wx.navigateToMiniProgram({
			appId: e.appid,
			path: e.path,
			success(res) {
				if (status.value == false) {
					status.value = true
					timeAdd()
				}
			},
			fail(err) {
				uni.showModal({
					title: '错误提示',
					content: "跳转失败" + err.errMsg,
					showCancel: false
				})
			}
		})
		// #endif
	}

	const play = () => {
		let task = storage.get(key)
		if (task.data.video_num >= WEBCONFIG?.watch_number) {
			$tool.toast('已达观看上限');
			return;
		}
		$videoAd.show()
	}

	const timeAdd = () => {
		if (status.value == true) {
			time.value++
			console.log(time.value)
			setTimeout(() => {
				timeAdd()
			}, 1000)
		}
	}


	onShareAppMessage(() => {
		let id = USERINFO.value.id || 0
		return {
			title: WEBCONFIG.value?.share_title || WEBCONFIG.value?.web_name || '',
			path: 'pages/index/index?id=' + id,
			imageUrl: WEBCONFIG.value?.share_image || ''
		}
	})
</script>

<style>
	.parent>view:not(:last-child) {
		border-bottom: 1px solid #363636;
	}

	.box {
		background-color: #fecc02;
	}

	.box-1 {
		background-color: #fee27a;
	}
</style>